package com.liba.logic.controller;

import com.liba.common.ResponseBean;
import com.liba.logic.entity.UserBean;
import com.liba.logic.service.UserService;
import com.liba.logic.utils.JwtUtils;
import com.liba.logic.utils.UserToken;
import com.liba.shiro.JWTToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;

@RestController
@Slf4j
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    public void setService(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/login")
    public ResponseBean login(@RequestParam("username") String username,
                              @RequestParam("password") String password) {

        Subject subject = SecurityUtils.getSubject();
        UserBean user=null;

        try {

            //将用户请求参数封装后，直接提交给Shiro处理
            subject.login(new UserToken(username, password, "Login"));

            //Shiro认证通过后会将user信息放到subject内，生成token并返回
            user = (UserBean) subject.getPrincipal();
            String newToken = userService.generateJwtToken(user.getUserName());
            log.info("newTokennewTokennewToken="+newToken);
            return new ResponseBean(200,"login success",newToken);

        } catch (AuthenticationException e) {

            // 如果校验失败，shiro会抛出异常，返回客户端失败
             e.printStackTrace();
            return new ResponseBean(500,"login fail","null");
        }


    }

    @PostMapping("/article")
    public ResponseBean article() {
        
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {





            return new ResponseBean(200, "You are already logged in", null);
        } else {
            return new ResponseBean(200, "You are guest", null);
        }
    }

    @GetMapping("/require_auth")
    @RequiresAuthentication
    public ResponseBean requireAuth() {
        return new ResponseBean(200, "You are authenticated", null);
    }

    @GetMapping("/require_role")
    @RequiresRoles("admin")
    public ResponseBean requireRole() {
        return new ResponseBean(200, "You are visiting require_role", null);
    }

    @GetMapping("/require_permission")
    @RequiresPermissions(logical = Logical.AND, value = {"view", "edit"})
    public ResponseBean requirePermission() {
        return new ResponseBean(200, "You are visiting permission require edit,view", null);
    }

    @RequestMapping(path = "/401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseBean unauthorized() {
        return new ResponseBean(401, "Unauthorized", null);
    }

}
